package com.zx.stock.service.impl;

import com.zx.stock.common.Const;
import com.zx.stock.entity.PerformanceData;
import com.zx.stock.exception.ChildThreadException;
import com.zx.stock.mapper.PerformanceDataMapper;
import com.zx.stock.mapper.PerformanceMapper;
import com.zx.stock.parallel.MultiParallelThreadHandler;
import com.zx.stock.parallel.MultiThreadHandler;
import com.zx.stock.service.PerformanceDataService;
import com.zx.stock.thread.PerformanceDataThread;
import com.zx.stock.thread.PerformanceThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class PerformanceDataServiceImpl implements PerformanceDataService {

    private static final  String per_date = "2021-12-31";
    private static final  Integer max_page = 80;
    private static final  Integer cur_page = 1;
    private static final  String per_url = "https://datacenter-web.eastmoney.com/api/data/v1/get?sortColumns=INCREASE_JZ&sortTypes=-1&pageSize=50&pageNumber=%s&reportName=RPT_PUBLIC_OP_NEWPREDICT&columns=ALL&filter=(REPORT_DATE%%3D'%s')(FORECAST_STATE%%3D%%22increase%%22)";


    @Resource
    private PerformanceDataMapper performanceDataMapper;

    @Resource
    private PerformanceMapper performanceMapper;

    //加载业绩数据
    @Override
    public void loadPerformanceTableDataList(){

        Map<String,Object> params = new HashMap<>();
        params.put("status", Const.N);
        params.put("noticeDate",per_date);
        List<PerformanceData> list = performanceDataMapper.list(params);

        if (list == null || list.isEmpty()){

            log.info("数据加载完成");
            return;
        }

        //多线程处理并发处理
        MultiThreadHandler handler = new MultiParallelThreadHandler();


        for (int i=0;i<20;i++){


            if (i<list.size()){
                PerformanceThread advanceConfigThread = new PerformanceThread(list.get(i),performanceDataMapper);
                handler.addTask(advanceConfigThread);
            }

        }
        try {
            handler.run();
        } catch (ChildThreadException e) {
            log.info(e.getAllStackTraceMessage());
        }

    }

    //初始化数据
    @Override
    public void initTableData(){

        List<PerformanceData> rsList = new ArrayList<>();
        for (int i=cur_page;i<max_page+1;i++){
            Integer key = Integer.valueOf(i);
            PerformanceData item = new PerformanceData();
            item.setCtime(new Date());
            item.setPage(key);
            item.setTotal(max_page);
            item.setUrl(String.format(per_url,i,per_date));
            item.setNoticeDate(per_date);
            item.setStatus(Const.N);
            //rsList.add(item);
            performanceDataMapper.insertDynamic(item);
        }
    }

    //加载业绩数据
    @Override
    public void loadPerformance(){

        Map<String,Object> params = new HashMap<>();
        params.put("status", Const.Y);
        params.put("noticeDate",per_date);
        List<PerformanceData> list = performanceDataMapper.list(params);

        if (list == null || list.isEmpty()){

            log.info("数据加载完成");
            return;
        }

        //多线程处理并发处理
        MultiThreadHandler handler = new MultiParallelThreadHandler();


        for (int i=0;i<20;i++){


            if (i<list.size()){
                PerformanceDataThread advanceConfigThread = new PerformanceDataThread(list.get(i),performanceDataMapper,performanceMapper);
                handler.addTask(advanceConfigThread);
            }

        }
        try {
            handler.run();
        } catch (ChildThreadException e) {
            log.info(e.getAllStackTraceMessage());
        }
    }
}
